package com.tengju.data.infrastructure.schedule;

import com.tengju.data.domain.model.alert.AlertEvent;
import com.tengju.data.domain.model.alert.AlertService;
import com.tengju.data.domain.model.report.AchievementBonusDateConfig;
import com.tengju.data.domain.model.service.AchievementBonusDateConfigService;
import com.tengju.data.domain.shared.CastUtil;
import com.tengju.data.domain.shared.Month;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @author chenhang
 * @Date 2021/6/22
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class BonusDateConfigSettleJob {

    private final AchievementBonusDateConfigService bonusDateConfigService;
    private final AlertService alertService;

    private final String jobName = "业绩奖励日期配置设置任务";

    /**
     * 每次设置未来近两个月的配置
     * 如果已经全部设置了，则跳过，否则添加默认设置
     * @param params
     * @return
     */
    @XxlJob("bonusDateConfigSettleJob")
    public ReturnT<String> execute(String params) {
        try {
            if (!StringUtils.isBlank(params)) {
                Month queryMonth = new Month(CastUtil.castInt(params));
                settleConfig(queryMonth);
                return ReturnT.SUCCESS;
            }
            Month curMonth = new Month(LocalDate.now());
            settleConfig(curMonth);
            settleConfig(curMonth.nextMonth());
        } catch (Exception e) {
            log.error("{}, jobError",jobName, e);
            AlertEvent event = new AlertEvent(jobName + "异常");
            event.addItem("任务参数",params);
            event.addItem("异常信息",e.getMessage());
            alertService.alert(event);
        }
        return ReturnT.SUCCESS;
    }

    private void settleConfig(Month month) {
        String dataKey = month.getMonthInt() + "";
        AchievementBonusDateConfig dateConfig = bonusDateConfigService.queryByDateKey(dataKey);
        boolean needSave = false;
        AlertEvent event = new AlertEvent(jobName + ":" + dataKey + ",已设置");
        if (dateConfig == null) {
            needSave = true;
            dateConfig = new AchievementBonusDateConfig(month.getRandomDate());
        }
        if (dateConfig.getStartDate() == null || dateConfig.getEndDate() == null) {
            needSave = true;
            dateConfig.setDefaultDateRange(month);
        }
        if (dateConfig.getSettleDate() == null) {
            needSave = true;
            dateConfig.setDefaultSettleCalDate(month);
        }
        if (dateConfig.getLastCalDate() == null) {
            needSave = true;
            dateConfig.setDefaultLastCalDate(month);
        }
        if (dateConfig.getRiskDate() == null) {
            needSave = true;
            dateConfig.setDefaultRiskDate(month);
        }
        if (needSave) {
            bonusDateConfigService.save(dateConfig);
            event.addItem("config",dateConfig.toString());
            alertService.alert(event);
        }
    }


}
